$(function(){
	crunchyroll_index.run();
});

var crunchyroll_index = {
	run: function(){
		this._init_();
		this._start_();
		this._bind_events();
	},
	
	_init_: function(){
		this.hasMore = $("#show_content_div > :hidden.hasMore").val() || false;
		this.category = $("#category").val();
		this.scrollPage = 2;
	},
	
	_start_: function(){
		this._highlight_nav();
		this._adjust_layout();
	},
	
	_bind_events: function(){
		$("#gotoTop").easeup();
		//scrolling
		$.scroll(function(){
			crunchyroll_index._handle_scrolling();
		});
		
		$(window).wresize(function() {
			crunchyroll_index._adjust_layout();
		});
		
		$("#open_side_btn").click(function(){
			crunchyroll_index._adjust_layout("open");
		});
		
		$("#close_side_btn").click(function(){
			crunchyroll_index._adjust_layout();
		});
		
		crunchyroll_index._handle_index_to_detail_location_param();
	},
	
	_highlight_nav: function(){
		var path = location.pathname;
		var index = 0;		
		if(path.startWith(main.contextPath + "/world/featured/chtv")){
			index = 1;
		}else if(path.startWith(main.contextPath + "/world/featured/jptv")){
			index = 2;
		}else if(path.startWith(main.contextPath + "/world/featured/krtv")){
			index = 3;
		}else if(path.startWith(main.contextPath + "/world/featured/othertv")){
			index = 4;
		}
		$("#side_menu > ul > li").eq(index).addClass("curr");
	},
	
	_handle_index_to_detail_location_param: function(){
		$("#show_content_div > div.m_box5 > div.img > a").click(function(){
			var href = $(this).attr("href");
			location.href = href + "#category=" + crunchyroll_index.category;
			return false;
		});
	},
	
	_handle_scrolling: function(){			
		if(this.hasMore){
			var $w = $(window);
			if( ! $w.data("scrolling")){
				$w.data("scrolling", true);
				crunchyroll_index._query_crunchyroll_tv(this.scrollPage);
			}
		}
	},
	
	_query_crunchyroll_tv: function(page){
		var $w = $(window);
		if($w.data("querying")) {
			return;
		}
		
		$w.data("querying", true);
		var $loading = $("#show_content_div > div.loadingBox");
		$loading.show();
		page = page || 1;
		var data = "category=" + this.category + "&page=" + page;
		var url = main.contextPath + "/world/featured/tv/query";
		$.get(url, data, function(result){
			crunchyroll_index._check_has_more(result);
			$loading.before(result);
		}).complete(function(){
			$w.data("querying", false);
			$w.data("scrolling", false);
			$loading.hide();
			//重新绑定事件
			crunchyroll_index._handle_index_to_detail_location_param();
			crunchyroll_index._adjust_layout();
		});
	},
	
	_check_has_more: function(result){
		var $result = $("<div>" + result + "</div>");
		var m = $(":hidden.hasMore", $result).val();
		if(m){
			this.hasMore = true;
			this.scrollPage += 1;
		}else{
			this.hasMore = false;
		}
	},
	
	_adjust_layout: function(action){
		var boxWidth = $("#show_content_div").width()-5;
		if(action == 'open'){
			boxWidth = boxWidth - 180;
		}
		var account = Math.floor(boxWidth/160);
		var paddingValue = 20 + (boxWidth - 160*account + 20)/(account-1);
		var flag = 1;
		$("#show_content_div > div.m_box5").each(function(){
			$(this).css("padding-right",0);
			if(flag < account){
				if(flag == 1){
					$(this).css("padding-left",0);
					flag++;
				}else{
					$(this).css("padding-left",paddingValue);
					flag++;
				}
			}else{
				$(this).css("padding-left",paddingValue);
				flag = 1;
			}
		});
	}
};